<?php

namespace Admin\Controller;

class MagazineController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Magazine");
    }


    public function index() {

        $list = D("Magazine")
            ->order("magazine_id asc")
            ->select();

        $this->assign('list', $list);
        $this->display();

    }

    public function addMagazine() {
        $magazineId = $_REQUEST["id"];
        $this->assign("magazineId", $magazineId);
        $this->display("addMagazine");
    }

    public function removeMagazine() {

        $id = $_REQUEST['id'];

        $magazine = D("Magazine")->queryById($id);

        $magazineId = $magazine["magazine_id"];

        D("Magazine")->deleteById($id);

        $this->success('删除成功!', U("magazine/index", array("id"=>$magazineId)));

    }


    public function editMagazine() {
        $id = $_REQUEST["id"];
        $magazine = D("Magazine")->where(array("magazine_id"=>$id))->find();
        $this->assign("info", $magazine);
        $this->display("editMagazine");
    }

    public function addMagazinePost() {

        $data["magazine_id"] = $_REQUEST["magazineId"];
        $data["create_time"] = getCurrentDate();
        $data["title"] = $_REQUEST["title"];
        $data["title_image"] = $_REQUEST["titleImage"];
        $data["disease"] = $_REQUEST["disease"];
        $data["type"] = $_REQUEST["type"];
        $data["content"] = html_entity_decode($_REQUEST["description"]);

        D("Magazine")->add($data);

        $this->success("添加成功", U("magazine/index", array("id"=>$_REQUEST["magazineId"])));

    }

    public function editMagazinePost() {

        $id = $_REQUEST["id"];

        $data = D("Magazine")->queryById($id);

        $data["title"] = $_REQUEST["title"];
        $data["create_time"] = getCurrentDate();
        $data["title_image"] = $_REQUEST["titleImage"];
        $data["disease"] = $_REQUEST["disease"];
        $data["type"] = $_REQUEST["type"];
        $data["content"] = html_entity_decode($_REQUEST["description"]);

        D("Magazine")->updateInfo($data);

        $this->success("更新成功", U("magazine/index", array("id"=>$data["magazine_id"])));

    }

}